package com.jqq.homework;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Homework02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            System.out.println("请输入用户名：");
            String name = scanner.next();
            isName(name);
            System.out.println("请输入密码：");
            String strPwd = scanner.next();
            isPassword(strPwd);
            System.out.println("请输入邮箱：");
            String email = scanner.next();
            isEmail(email);
            System.out.println("注册成功！");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("注册失败！");
        }
    }

    public static void isEmail(String str) {
        int i = str.indexOf('@');
        int j = str.indexOf('.');
        if (!(i > 0 && j > i)) {
            throw new RuntimeException("邮箱格式不正确");
        }
    }

    public static void isName(String name) {
        if (!(name.length() >= 2 && name.length() <= 4)) {
            throw new RuntimeException("用户名长度不正确");
        }
    }

    public static void isPassword(String strPwd) {
        Pattern pattern = Pattern.compile("\\d{6}");
        Matcher matcher = pattern.matcher(strPwd);
        if (!(matcher.matches())) {
            throw new RuntimeException("密码格式不正确");
        }
    }
}
